

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gczn_hd/app/core/base/handle_mixin.dart';
import 'package:gczn_hd/app/core/values/App_img.dart';
import 'package:gczn_hd/app/core/values/app_colors.dart';
import 'package:gczn_hd/app/core/values/app_values.dart';
import 'package:gczn_hd/app/core/widget/custom_title_bar.dart';
import 'package:gczn_hd/app/modules/ppm/station/widget/task_awaiting.dart';
import 'package:gczn_hd/app/routes/app_pages.dart';
import 'package:get/get.dart';

import '../../../../core/values/text_styles.dart';

class StationHandle extends StatelessWidget with HandleMixin{



  @override
  Widget build(BuildContext context) {

    return SingleChildScrollView(
      child: Column(
        children: [
          _buildTaskMessage(),
          SizedBox(height: AppValues.margin_10.h,),
          TaskAwaiting(), // 待产任务
        ],
      ),
    );
  }

  Widget _buildTaskMessage(){
    const boxSize = Size(513.0, 544.0);

    return Container(
      width: double.infinity,
      height: boxSize.height.h,
      decoration: const BoxDecoration(
        image:  DecorationImage(
          image: AssetImage(AppImg.inProduction),
          fit: BoxFit.fill,
          alignment: Alignment.centerRight,
        ),
      ),
      child: Row(
        children: [
          Expanded(
            flex: 1,
            child: _buildMessage(),
          ),
          Container(
            width: boxSize.width.w,
            padding: EdgeInsets.only(top: AppValues.size28.h, left: AppValues.size30.w, right: AppValues.size30.w),
            child: _buildHandleBar(),
          )
        ],
      ),
    );

  }

  Widget _buildHandleBar(){

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        CustomTitleBar(title: "功能面板", handleBtn: GestureDetector(
          onTap: (){
            showToast("点一点250");
          },
          child: ImageIcon(Image.asset(AppImg.dot).image, size: AppValues.size37.w, color: AppColors.colorWhite,),
        ),),

        Expanded(
          flex: 1,
          child: Container(
            margin: EdgeInsets.only(top: AppValues.size38.h, bottom: AppValues.size38.h),
            child: _buildGrid(),
          ),
        )
      ],
    );

  }


  Widget _buildGrid(){
    final List<Map<String, String>> gridItems = [
      {'title': 'QA', 'subtitle': '检验',"isDisable": '0'},
      {'title': 'QC', 'subtitle': '检验', "isDisable": '1'},
      {'title': '生产', 'subtitle': '报工', "isDisable": '0'},
      {'title': '查看', 'subtitle': 'SOP', "isDisable": '0'},
    ];

    return GridView.builder(
      padding: EdgeInsets.zero,
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        crossAxisSpacing: 10.0,
        mainAxisSpacing: 10.0,
        childAspectRatio: 1.0,
      ),
      itemCount: gridItems.length,
      itemBuilder: (context, index) {
        return  GestureDetector(
          onTap: () {
            if( gridItems[index]['subtitle'] == '报工' ){
              Get.toNamed(Routes.REPORT);
            }
            if(  gridItems[index]['subtitle'] == '检验' ){
              Get.toNamed(Routes.QC_CHECK_LIST);
            }
            showToast(gridItems[index]['title']!);
          },
          child: Card(
            elevation: 2.0,
            color: gridItems[index]['isDisable'] == '0' ? AppColors.cardItem : AppColors.cardItem2, // 设置背景色
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(gridItems[index]['title']!, style: textStylebtn),
                  Text(gridItems[index]['subtitle']!, style: textStylebtn),
                ],
              ),
            ),
          ),
        );
      },
    );
  }


  /// 左侧信息
  Widget _buildMessage(){
    const boxSize = Size(28.0, 32.0);

    return Container(
      padding: EdgeInsets.only(top: AppValues.size28.h, left: AppValues.size30.w, right: AppValues.size30.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          CustomTitleBar(title: "在产任务", handleBtn: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                width: boxSize.width.w, height: boxSize.height.h,
                child: Stack(
                  children: [
                    Positioned(
                      child: Image.asset(AppImg.alarm, width: boxSize.width.w, height: boxSize.height.h,),
                    ),
                    Center(
                      child: Text("10", style: submitButtonText.copyWith(fontSize: AppValues.margin_12.sp),),
                    )
                  ],
                ),
              ),
              SizedBox(width: AppValues.margin_10.w,),
              Text("生产进度缓慢！预计延期2日！", style: dangerText,),
              const Icon(Icons.arrow_forward_ios, size: AppValues.size16, color: AppColors.colorWhite,)
            ],
          ),),
          Padding(
            padding: EdgeInsets.only(left: AppValues.size20.w, top: AppValues.size20.h),
            child: Text("在制品：格力空气循环落地风扇-底座塑件  颜色:清巧银+黑色,智控方式:语音", style: textStyle,),
          ),
          SizedBox(height: AppValues.size30.h,),
          _buildYield(),
          SizedBox(height: AppValues.size30.h,),
          _buildProgress(),
          SizedBox(height: AppValues.size30.h,),
          _buildTableMessage()
        ],
      ),
    );
  }

  /// 表格信息
  Widget _buildTableMessage(){
    List<Map<String, String>> list = [
      {"name": " 任务单号", "value": "RO20241212001"},
      {"name": " 所属产线", "value": "小牛落地风扇生产线"},
      {"name": " 所属车间", "value": "智能工厂/三号车间"},
      {"name": " 执行工位", "value": "工位C00001"},
      {"name": " 通讯设备", "value": "注塑机05"},
      {"name": " 辅助设备", "value": "D20240006"},
      {"name": " 计划起止时间", "value": "05-31 14:00 ~ 05-31 20:00"},
      {"name": " 实际起止时间", "value": "05-31 15:25.~"},
    ];

    return Table(
      border: TableBorder.all(color: AppColors.tableBorder, borderRadius: BorderRadius.circular(AppValues.margin_12.r), width: AppValues.decimals5),
      columnWidths: const {
        0: FlexColumnWidth(1.5),
        1: FlexColumnWidth(3),
        2: FlexColumnWidth(1.5),
        3: FlexColumnWidth(3),
      },
      children: _buildTableRow(list),
    );
  }

  List<TableRow> _buildTableRow(List<Map<String, String>> list){
    List<TableRow> rows = [];

    for (int i = 0; i < list.length; i += 2) {
      Widget firstCellName = Container(
        color: AppColors.tableCellBg,
        padding: EdgeInsets.all(AppValues.margin_12.w),
        child: Text(list[i]['name'] ?? '', style: textStyle20),
      );
      Widget firstCellValue = Padding(
        padding: EdgeInsets.all(AppValues.margin_12.w),
        child: Text(list[i]['value'] ?? '', style: textStyle20),
      );

      Widget secondCellName = Container();
      Widget secondCellValue = Container();

      if (i + 1 < list.length) {
        secondCellName = Container(
          color: AppColors.tableCellBg,
          padding: EdgeInsets.all(AppValues.margin_12.w),
          child: Text(list[i + 1]['name'] ?? '', style: textStyle20),
        );
        secondCellValue = Padding(
          padding: EdgeInsets.all(AppValues.margin_12.w),
          child: Text(list[i + 1]['value'] ?? '', style: textStyle20),
        );
      }

      rows.add(
        TableRow(
          children: [
            firstCellName,
            firstCellValue,
            secondCellName,
            secondCellValue,
          ],
        ),
      );
    }

    return rows;

  }

  /// 进度条
  Widget _buildProgress(){
    const double progress = 0.25;

    return Padding(
      padding: EdgeInsets.only(left: AppValues.size20.w),
      child: Row(
        children: [
          Text("生产进度:", style: textStyle.copyWith(fontSize: AppValues.size20.sp),),
          const SizedBox(width: 10,),
          Expanded(
            flex: 1,
            child: ShaderMask(
              shaderCallback: (Rect bounds) {
                return const LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    Color.fromRGBO(23, 77, 132, 1),
                    Color.fromRGBO(75, 147, 237, 1),
                  ],
                ).createShader(bounds);
              },
              child: SizedBox(
                height: AppValues.size24.h,
                child: LinearProgressIndicator(
                  value: progress, // 假设进度为26.23%
                  backgroundColor: const Color(0xFF192933),
                  minHeight: AppValues.size20.h,
                  borderRadius: BorderRadius.circular(AppValues.margin_10.r),
                  valueColor: const AlwaysStoppedAnimation<Color>(Colors.white), // 使用白色作为占位颜色
                ),
              ),
            ),
          ),
          const SizedBox(width: 10,),
          Text("26.23%", style: textStyle,)
        ],
      ),
    );
  }

  // 产量明细
  Widget _buildYield(){

    return Padding(
      padding: EdgeInsets.only(left: AppValues.size20.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildYieldItem(title: "计划产量", value: "1000台" ),
          _buildYieldItem(title: "自动计数", value: "32台" ),
          _buildYieldItem(title: "报工数量", value: "30台" ),
          _buildYieldItem(title: "不良数量", value: "2台" ),
        ],
      ),
    );
  }

  Widget _buildYieldItem({required String title, required String value}){

    return Expanded(
      flex: 1,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(value, textAlign: TextAlign.center, style: textStyle, ),
          SizedBox(height: 8.h,),
          Text(title, style: textStyle.copyWith(fontSize: AppValues.size20.sp), textAlign: TextAlign.center,)
        ],
      ),
    );

  }


}




